package com.open.loan.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.open.loan.domain.rsp.RspProductDetailModel;
import com.open.loan.domain.rsp.RspProductInfoModel;
import com.open.loan.domain.rsp.RspShopInfoModel;
import com.open.loan.utils.ResponseUtil;
import com.open.system.domain.LoanBankDO;
import com.open.system.domain.LoanProductDO;
import com.open.system.domain.LoanShopDO;
import com.open.system.domain.LoanUserDO;
import com.open.system.service.LoanBankService;
import com.open.system.service.LoanProductService;
import com.open.system.service.LoanShopService;
import com.open.system.service.LoanUserService;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/moblie")
public class VueProductPostController {

	@Autowired
	private LoanProductService loanProductService;
	
	@Autowired
	private LoanShopService loanShopService;
	
	@Autowired
	private LoanUserService userService;
	
	@Autowired
	private LoanBankService LoanBankService;
	
	@ApiOperation(value="首页", notes="")
	@RequestMapping(value ="/homeList/{shopId}",method = RequestMethod.GET)
	public ResponseEntity<String> homeList(@PathVariable(value = "shopId") String shopId,HttpServletRequest request,HttpServletResponse response){
				try{
					HttpSession session = request.getSession();
					LoanUserDO loanUserDo=(LoanUserDO) session.getAttribute("loanUserDo");
					if(loanUserDo==null){
						ResponseUtil.toWrite("100","请重新登录",null, response);
						return null;
					}
					
					if(StringUtils.isEmpty(shopId)){
						ResponseUtil.toWrite("1000","店铺ID不能为空",null, response);
						return null;
					}
					Pattern pattern = Pattern.compile("^-?[0-9]+");
					 if(!pattern.matcher(shopId).matches()){
				            shopId="1";
				        }
					
					Map<String,Object>mapJsons=new HashMap<String,Object>();
					RspShopInfoModel shopModel=new RspShopInfoModel();
					LoanShopDO loanShopDO=loanShopService.get(Long.parseLong(shopId));
					if(loanShopDO!=null){
						shopModel.setShopAddress(loanShopDO.getShopAddress());
						shopModel.setShopContact(loanShopDO.getShopContact());
						shopModel.setShopName(loanShopDO.getShopName());
						shopModel.setShopNumber(loanShopDO.getShopNumber());
						mapJsons.put("shopInfo",shopModel);
					}else {
						mapJsons.put("shopInfo",1000);
					}
					List<RspProductInfoModel>productMapS=new ArrayList<RspProductInfoModel>();
					Map<String, Object> map=new HashMap<String, Object>();
					map.put("remark2", shopId);
					map.put("status","0");
					List<LoanProductDO>productData=loanProductService.list(map);
					for (int i = 0; i <productData.size(); i++) {
						RspProductInfoModel model=new RspProductInfoModel();
						LoanProductDO product=productData.get(i);
						model.setHasHot(product.getHasHot());
						model.setStatus(product.getStatus());
						model.setProductName(product.getProductName());
						model.setProductImage(product.getProductImage());
						model.setProductId(String.valueOf(product.getId()));
						productMapS.add(model);
					}
					mapJsons.put("productInfo",productMapS);
					ResponseUtil.toWrite("200","获取产品列表成功",mapJsons, response);
					return null;
				} catch (Exception e) {
					e.printStackTrace();
					ResponseUtil.toWrite("202","获取产品失败",null, response);
					return null;
				}
		}
	
	//--产品详情------------------------------------------------------------------------------------------
	@ApiOperation(value="产品列表", notes="")
	@RequestMapping(value ="/productList/{shopId}",method = RequestMethod.GET)
	public ResponseEntity<String> productList(@PathVariable(value = "shopId") String shopId,HttpServletResponse response){
				try{
					Map<String,Object>mapJsons=new HashMap<String,Object>();
					List<RspProductDetailModel>productMapS=new ArrayList<RspProductDetailModel>();
					Map<String, Object> map=new HashMap<String, Object>();
					map.put("remark2", shopId);
					map.put("status","0");
					List<LoanProductDO>productData=loanProductService.list(map);
					for (int i = 0; i <productData.size(); i++) {
						RspProductDetailModel model=new RspProductDetailModel();
						LoanProductDO product=productData.get(i);
						model.setProductName(product.getProductName());
						model.setProductImage(product.getProductImage());
						model.setLoanDays(product.getLoanDays());
						model.setLoanRate(product.getLoanRate());
						model.setProductId(String.valueOf(product.getId()));
						productMapS.add(model);
					}
					mapJsons.put("productInfo",productMapS);
					ResponseUtil.toWrite("200","获取产品列表成功",mapJsons, response);
					return null;
				} catch (Exception e) {
					e.printStackTrace();
					ResponseUtil.toWrite("202","获取详情失败",null, response);
					return null;
				}
		}
	
	
	@ApiOperation(value="产品详情", notes="")
	@RequestMapping(value ="/productDetails",method = RequestMethod.GET)
	public ResponseEntity<String> productDetails(String productId,String shopId,HttpServletRequest request,HttpServletResponse response){
				try{
					HttpSession session = request.getSession();
					LoanUserDO loanUserDo=(LoanUserDO) session.getAttribute("loanUserDo");
					if(loanUserDo==null){
						ResponseUtil.toWrite("100","请重新登录",null, response);
						return null;
					}
					
					if(StringUtils.isEmpty(productId)){
						ResponseUtil.toWrite("1001","产品ID不能为空",null, response);
						return null;
					} 
					LoanProductDO productDO=loanProductService.get(Long.parseLong(productId));
					if(productDO==null){
						ResponseUtil.toWrite("1002","该产品不存在或已下架",null, response);
						return null;
					}
					if(StringUtils.isEmpty(shopId)){
						ResponseUtil.toWrite("1000","店铺ID不能为空",null, response);
						return null;
					}
					if(StringUtils.isEmpty(productDO.getRemark1())){
						ResponseUtil.toWrite("1009","合同暂无发布或审核中..",null, response);
						return null;
					}
					if(!"1".equals(productDO.getRemark1())){
						ResponseUtil.toWrite("1009","合同暂无发布或审核中..",null, response);
						return null;
					}
					Map<String,Object>bankMap=new HashMap<String,Object>();
					bankMap.put("userId", loanUserDo.getId());
					List<LoanBankDO>banks=LoanBankService.list(bankMap);
					LoanBankDO bankDO=null;
					if(banks!=null && banks.size()>0){
						bankDO=banks.get(0);
					}else {
						ResponseUtil.toWrite("103","请先实名认证",null, response);
						return null;
					}
					
					Map<String,Object>mapJsons=new HashMap<String,Object>();
					RspShopInfoModel shopModel=new RspShopInfoModel();
					LoanShopDO loanShopDO=loanShopService.get(Long.parseLong(shopId));
					if(loanShopDO!=null){
						shopModel.setShopAddress(loanShopDO.getShopAddress());
						shopModel.setShopContact(loanShopDO.getShopContact());
						shopModel.setShopName(loanShopDO.getShopName());
						shopModel.setShopNumber(loanShopDO.getShopNumber());
						mapJsons.put("shopInfo",shopModel);
					}else {
						mapJsons.put("shopInfo",1000);
					}
					RspProductDetailModel model=new RspProductDetailModel();
					model.setProductName(productDO.getProductName());
					model.setProductImage(productDO.getProductImage());
					model.setLoanRate(productDO.getLoanRate());
					model.setLoanDays(productDO.getLoanDays());
					String contract=productDO.getContract();
					//备注：身份证标识: AAAAA , 姓名: BBBBB , 手机号: CCCCC , 时间: DDDDD 】
					contract=contract.replaceAll("AAAAA", bankDO.getIdNo());
					contract=contract.replaceAll("BBBBB", bankDO.getRealName());
					contract=contract.replaceAll("CCCCC", bankDO.getMoblie());
					Date currentTime = new Date();
				    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				    String dateString = formatter.format(currentTime);
				    contract=contract.replaceAll("DDDDD", dateString);
				    contract=contract.replaceAll("EEEEE", loanUserDo.getCensusAddress());
					model.setContract(contract);
					mapJsons.put("productDetail", model);
					mapJsons.put("createTime", dateString);
					ResponseUtil.toWrite("200","获取详情成功",mapJsons, response);
					return null;
				} catch (Exception e) {
					e.printStackTrace();
					ResponseUtil.toWrite("202","获取详情失败",null, response);
					return null;
				}
		}
	
}
